import useKnowledgeStore from "@/store/knowledge";
import useUserInfoStore from "@/store/userInfo";

export function $permissionRole(str,roleArr = [],isAll = false){
    const knowAuth = useKnowledgeStore()
    // 超级管理员具备所有权限
    // if(store.getters.userInfo.adminFlag) {
    //   return true
    // }
    let t = false;
    let newAuthArr = JSON.parse(JSON.stringify(knowAuth.konwledgeAuth))
    if(roleArr.length!=0){
      newAuthArr = []
      roleArr.forEach((item,index)=>{
        if(item.isSelect){
          newAuthArr.push(item.identifyingKey)
        }
      })
    }
    if(newAuthArr.length==0) {
      return false
    }
    if(Object.prototype.toString.call(str) == '[object String]'){
      if (newAuthArr.indexOf(str) == -1) {
        if (!str || str == '') {
          t = true;
        }else {
          t = false
        }
      } else {
        t = true
      }
    }else if(Object.prototype.toString.call(str) == '[object Array]'){
      const filteredArray = newAuthArr.filter(value => str.indexOf(value) !== -1);
      if(isAll){
        t = filteredArray.length == str.length
      }else{
        t = filteredArray.length > 0
      }
    }
    return t
}

export function permissionMatch(str){
  let userStore = useUserInfoStore()
  let t = false
  if(!userStore.permissions) {
    return false
  }
  if (userStore.permissions.indexOf(str) == -1) {
    if (!str || str == '') {
      t = true;
    }else {
      t = false
    }
  } else {
    t = true
  }
  return t
}